const app = getApp();
timer = require('../../plugins/wxTimer.js')
var apps = getApp().globalData,
    timer = apps.timer;
var rewardedVideoAd = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        info: {},
        selKisim: 1,
        playerUrl: '',
        jamiwakit: "300",
        kalganwakit: '',
        elankoruldi: false,
        videoCtx: '',
        haklik: false,
        kirmigan: false,
        askartix: false,
        kirix_eskartix: false,
        haklik_kino:false,
        kino_id: '',
        wxTimerList:{},
        tewsiya_kino:[],
        sahlandi:false,
        haliti:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            kino_id: options.kino_id
        });
        
        this.korluxi();
        this.korganlirim();
        //获得popup组件
        this.popup = this.selectComponent("#popup");
        this.setData({
            videoCtx: wx.createVideoContext('myVideo', this)
        })

        // 页面加载提示
        wx.showLoading({
            title: 'يۈكلىنىۋاتىدۇ',
        })

        var kino_id = this.data.kino_id
        this.GetKinoinfo(kino_id = kino_id);

        this.checklogin();

    },

    // 获取Kinoxinxi
    GetKinoinfo: function (kino_id) {
        var openid = wx.getStorageSync('userinfo').openid;
        // 获取kino信息
        wx.request({
            url: app.globalData.Host_url + 'Kino/GetKinoInfo?kino_id=' + kino_id + '&openid=' + openid,
            method: "GET",
            success: (res) => {
                if (res.data.code === 200) {
                    this.setData({
                        info: res.data.data,
                        sahlandi: res.data.data.sahlandi
                    });
                    setTimeout(function () {
                        wx.hideLoading()
                    }, 100)
                    // 修改页面标题
                    wx.setNavigationBarTitle({
                        title: res.data.data.kino_name + " تەپسىلاتى",
                    });
                    this.getPlayerUrl();
                    var vip = wx.getStorageSync('userinfo').vip;
                    if (vip === 0 || vip === undefined) {
                        if (res.data.data.bahasi !== 0 || res.data.data.qaklimisi === 2) {
                            this.setData({
                                haklik: true
                            });
                        }else{
                            this.GetElan();
                        }
                        
                    }
                    wx.request({
                      url: app.globalData.Host_url + 'Kino/getTewsiyaKino?tur_id=' + res.data.data.turi,
                      method:"GET",
                      dataType:"json",
                      success:(res)=>{
                        this.setData({
                            tewsiya_kino:res.data.data
                        })
                      }
                    })
                } else {
                    wx.showToast({
                        title: '请求失败',
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        });
    },

    // 访问数he观看记录
    korluxi:function(){
        var kino_id = this.data.kino_id;
        var openid = wx.getStorageSync('userinfo').openid;
        var wxTimer = new timer({
            beginTime:"00:00:30",
            complete:function(){
                wx.request({
                  url: app.globalData.Host_url + 'Kino/AddKinoView?kino_id=' + kino_id,
                  success:(res)=>{}
                });
            }
        })
        wxTimer.start(this);
    },
    

    // 观看记录
    korganlirim:function(){
        var kino_id = this.data.kino_id;
        var openid = wx.getStorageSync('userinfo').openid;
        var wxTimer = new timer({
            beginTime:"00:00:30",
            complete:function(){
                wx.request({
                    url: app.globalData.Host_url + 'Kino/setKorganlirim?openid=' + openid + '&kino_id=' + kino_id,
                    success:(res)=>{}
                });
            }
        })
        wxTimer.start(this);
    },


    // 判断影集是否VIP
    checkvip:function(res){
        var kirmigan = this.data.kirmigan;
        var kinoinfo = this.data.info;
        var vipkun = wx.getStorageSync('userinfo').vip;
        if (!kirmigan) {
            if (kinoinfo.bahasi !== 0 || kinoinfo.qaklimisi === 2) {
                if (vipkun === 0) {
                    this.setData({
                        haklik: false,
                        askartix: true,
                        haklik_kino:true
                    })
                    this.data.videoCtx.pause()
                } else {
                    var e = res.currentTarget.dataset.id;
                    this.getPlayerUrl(e);
                }
            } else {
                var e = res.currentTarget.dataset.id;
                this.getPlayerUrl(e);
            }
        }else{
            this.data.videoCtx.pause()
            var askartix = this.data.askartix;
            if (!askartix) {
                this.GetUpdateTime();
                this.setData({
                    askartix: true,
                    kirix_eskartix: true
                });
            }
        }
    },


    // 加载广告组件
    GetElan: function () {
        if (wx.createRewardedVideoAd) {
            rewardedVideoAd = wx.createRewardedVideoAd({
                adUnitId: 'adunit-61a7c7d5c701d2cd'
            })
            rewardedVideoAd.onLoad(() => {})
            rewardedVideoAd.onError((err) => {})
            rewardedVideoAd.onClose((res) => {
                if (res.isEnded) {
                    this.data.videoCtx.play()
                    this.setData({
                        elankoruldi: true
                    })
                } else {
                    this.toastDialog.showDialog('كىچىككىنە بىر ئېلان كۆرۈۋېتىڭە ئۇندا قىمماي');
                }
            })
        }
    },

    // 判断用户是否登录
    checklogin: function () {
        // 判断是否登录状态
        var userinfo = wx.getStorageSync('userinfo');
        if (!userinfo) {
            this.setData({
                kirmigan: true
            });
        }else{
            this.setData({
                kirmigan: false
            });
        }
    },


    // 获取Kino地址
    getPlayerUrl(e) {
        if (e === undefined) {
            var kisim_uid = 1;
        } else {
            var kisim_uid = e;
        }
        var kino_id = this.data.info.kino_id;
        wx.request({
            url: app.globalData.Host_url + 'Kino/getPlayerUrl?kisim_uid=' + kisim_uid + '&kino_id=' + kino_id,
            method: "GET",
            success: (res) => {
                if (res.data.code === 200) {
                    this.setData({
                        playerUrl: res.data.data.PlayerUrl,
                        selKisim: res.data.data.kisim_id
                    })
                } else {
                    wx.showToast({
                        title: '请求失败',
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        })
    },

    // 视频进度计算
    GetUpdateTime: function (e) {
        if (e === undefined) {
            e = 0
        } else {
            //实时播放进度 秒数
            var currentTime = parseInt(e.detail.currentTime);
        }
        var jamiwakit = this.data.jamiwakit;
        var kalganwakit = jamiwakit - currentTime;
        var elankoruldi = this.data.elankoruldi;
        var kirmigan = this.data.kirmigan;
        var vipkun = wx.getStorageSync('userinfo').vip;
        var info = this.data.info;
        if (vipkun === 0 || kirmigan === true) {
            if (info.bahasi !== 0 || info.qaklimisi === 2) {
                if (kalganwakit <= 0) {
                    this.setData({
                        haklik: false,
                        askartix: true,
                        haklik_kino:true,
                        kirix_eskartix: false
                    })
                    this.data.videoCtx.pause()
                }
            } else {
                if (kalganwakit <= 0) {
                    var kirmigan = this.data.kirmigan
                    if (kirmigan) {
                        this.setData({
                            askartix: true,
                            kirix_eskartix: true
                        });
                        this.data.videoCtx.pause();
                    } else {
                        if (elankoruldi === false) {
                            this.data.videoCtx.pause();
                            this.setData({
                                haklik: false
                            })
                            rewardedVideoAd.show();
                        }
                    }
                }

            }
        }
        var minute = 0; // 分
        var hour = 0; //小时
        if (kalganwakit > 60) {
            minute = parseInt(kalganwakit / 60);
            kalganwakit = parseInt(kalganwakit % 60);
            if (minute > 60) {
                hour = parseInt(minute / 60);
                middle = parseInt(minute % 60);
            }
        }
        var result = "" + parseInt(kalganwakit) + " سىكونت "
        if (minute > 0) {
            result = "" + parseInt(minute) + " مىنۇت " + result;
        }
        if (hour > 0) {
            result = "" + parseInt(hour) + " سائەت " + result;
        }
        this.setData({
            kalganwakit: result
        });
    },

     // 获取用户信息
     getUserInfo: function (res) {
        wx.getUserProfile({
            desc: '用于完善会员资料',
            success:(res)=>{
                var userinfo = res.userInfo
                var that = this;
                wx.login({
                    success(res) {
                        var data = {
                            "username":userinfo['nickName'],
                            "jinsi":userinfo['gender'],
                            "avatarUrl":userinfo['avatarUrl'],
                            "code":res.code
                        };
                        if (res.code) {
                            //发起网络请求
                            wx.request({
                                url: app.globalData.Host_url + 'Login/login',
                                data: {data:data},
                                method:"POST",
                                dataType:"json",
                                success: (res) => {
                                    if (res.data.code === 200) {
                                        wx.setStorageSync('userinfo',res.data.data);
                                        that.setData({
                                            askartix:false,
                                            kirmigan: false
                                        })
                                        that.GetUpdateTime()
                                        that.data.videoCtx.play();
                                        that.korganlirim();
                                    }
                                }
                            })
                        } else {
                            console.log('登录失败！' + res.errMsg)
                        }
                    }
                })
            }
        })
        
    },

    // vip eqix
    Vip_Eqix:function(){
        console.log('vip');
    },

    // setiwelix
    Setiwelix:function(e){
        console.log('setiwelix');
    },
    
    // 获取电影
    kinoinfo:function(e){
        var options = {
            kino_id:e.currentTarget.dataset.id,
            kino_name: e.currentTarget.dataset.name
        };
        this.onLoad(options);
    },

    // 收藏
    sahliwelix:function(){
        var kino_id = this.data.kino_id;
        var openid = wx.getStorageSync('userinfo').openid;
        wx.request({
            url: app.globalData.Host_url + 'Kino/sahliwelix?openid=' + openid + '&kino_id=' + kino_id,
            success:(res)=>{
                if (res.data === 0) {
                    this.data.videoCtx.pause()
                    var askartix = this.data.askartix;
                    if (!askartix) {
                        this.GetUpdateTime();
                        this.setData({
                            askartix: true,
                            kirix_eskartix: true
                        });
                    }
                }else if(res.data === 1){
                    this.setData({
                        sahlandi:false
                    })
                }else{
                    this.setData({
                        sahlandi:true
                    })
                }
            }
        });
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function (e) {
        //获得dialog组件 
        this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (e) {
        let _this = this;
         // 获取小程序配置
         wx.request({
            url: app.globalData.Host_url + '/Index/index',
            method:"GET",
            success:function(res){
                console.log(res.data.data);
              wx.setStorageSync('userip', res.data.data.ip);
              _this.onLoad();
              _this.setData({
                haliti: res.data.data.haliti,
              })
              _this.getTabBar().setData({
                haliti: res.data.data.haliti
              })
            }
          })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})